//-----------------------------------------------------------------------
// <copyright file="ObjectSub_AdessaDAOBase.cs" company="FetCo">
//     Copyright (c) FetCo. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using FetCo.Framework;
using FetCo.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ADESSA.Entities;

namespace ADESSA.Data
{    
    public abstract class ObjectSub_AdessaDAOBase
    {
        #region Common methods
        public virtual ObjectSub_Adessa CreateObjectSub_AdessaFromReader(IDataReader reader)
        {
            ObjectSub_Adessa item = new ObjectSub_Adessa();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("Tag_TGKM_ObjectId"))) item.Tag_TGKM_ObjectId = (long)reader["Tag_TGKM_ObjectId"];
                if (!reader.IsDBNull(reader.GetOrdinal("TagId"))) item.TagId = (long)reader["TagId"];
                if (!reader.IsDBNull(reader.GetOrdinal("TGKM_ObjectId"))) item.TGKM_ObjectId = (long)reader["TGKM_ObjectId"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateObjectSub_AdessaFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateObjectSub_Adessa methods
            
        public virtual void CreateObjectSub_Adessa(ObjectSub_Adessa objectSub_Adessa)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spObjectSub_AdessaInsert");
                
                database.AddInParameter(dbCommand, "@TagId", DbType.Int64, objectSub_Adessa.TagId);
                database.AddInParameter(dbCommand, "@TGKM_ObjectId", DbType.Int64, objectSub_Adessa.TGKM_ObjectId);
                database.AddOutParameter(dbCommand, "@Tag_TGKM_ObjectId", DbType.Int64, 8);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                objectSub_Adessa.Tag_TGKM_ObjectId = (long)database.GetParameterValue(dbCommand, "@Tag_TGKM_ObjectId");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateObjectSub_AdessaException, ex);
            }
        }

        #endregion

        #region UpdateObjectSub_Adessa methods
        
        public virtual void UpdateObjectSub_Adessa(ObjectSub_Adessa objectSub_Adessa)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spObjectSub_AdessaUpdate");            
                
                database.AddInParameter(dbCommand, "@Tag_TGKM_ObjectId", DbType.Int64, objectSub_Adessa.Tag_TGKM_ObjectId);
                database.AddInParameter(dbCommand, "@TagId", DbType.Int64, objectSub_Adessa.TagId);
                database.AddInParameter(dbCommand, "@TGKM_ObjectId", DbType.Int64, objectSub_Adessa.TGKM_ObjectId);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateObjectSub_AdessaException, ex);
            }
        }
        
        #endregion

        #region DeleteObjectSub_Adessa methods
        public virtual void DeleteObjectSub_Adessa(long tag_TGKM_ObjectId)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spObjectSub_AdessaDelete");
                
                database.AddInParameter(dbCommand, "@Tag_TGKM_ObjectId", DbType.Int64, tag_TGKM_ObjectId);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteObjectSub_AdessaException, ex);
            }
        }

        #endregion

        #region GetObjectSub_Adessa methods
        
        public virtual ObjectSub_Adessa GetObjectSub_Adessa(long tag_TGKM_ObjectId)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spObjectSub_AdessaGet");
                
                database.AddInParameter(dbCommand, "@Tag_TGKM_ObjectId", DbType.Int64, tag_TGKM_ObjectId);
                
                ObjectSub_Adessa objectSub_Adessa =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        objectSub_Adessa = CreateObjectSub_AdessaFromReader(reader);
                        reader.Close();
                    }
                }
                return objectSub_Adessa;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetObjectSub_AdessaException, ex);
            }
        }
        
        #endregion

        #region GetObjectSub_AdessaList methods
        public virtual ObjectSub_AdessaCollection GetObjectSub_AdessaList(ObjectSub_AdessaColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spObjectSub_AdessaGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                ObjectSub_AdessaCollection objectSub_AdessaCollection = new ObjectSub_AdessaCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        ObjectSub_Adessa objectSub_Adessa = CreateObjectSub_AdessaFromReader(reader);
                        objectSub_AdessaCollection.Add(objectSub_Adessa);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return objectSub_AdessaCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetObjectSub_AdessaListException, ex);
            }
        }
        
        public virtual ObjectSub_AdessaCollection GetObjectSub_AdessaList(ObjectSub_AdessaColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetObjectSub_AdessaList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
		  #region GetObjectSub_AdessaList methods
        public virtual ObjectSub_AdessaCollection GetObjectSub_AdessaListByParam(object orderBy, OrderDirection orderDirection, int page, int pageSize,string Criteria, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spObjectSub_AdessaGetListByParam");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
				database.AddInParameter(dbCommand, "@Criteria",  DbType.String, Criteria);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                ObjectSub_AdessaCollection objectSub_AdessaCollection = new ObjectSub_AdessaCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        ObjectSub_Adessa objectSub_Adessa = CreateObjectSub_AdessaFromReader(reader);
                        objectSub_AdessaCollection.Add(objectSub_Adessa);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return objectSub_AdessaCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetObjectSub_AdessaListException, ex);
            }
        }
        
        public virtual ObjectSub_AdessaCollection GetObjectSub_AdessaListByParam(object orderBy, OrderDirection orderDirection,string Criteria)
        {            
            int totalRecords = 0;
            return GetObjectSub_AdessaListByParam(orderBy, orderDirection, 0, 0,Criteria, out totalRecords);
        }
        
        #endregion
    }
}